package com.qiange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiange.common.BaseContext;
import com.qiange.common.Result;
import com.qiange.pojo.AddressBook;
import com.qiange.pojo.OrderDetail;
import com.qiange.pojo.Orders;
import com.qiange.dto.OrdersDto;
import com.qiange.pojo.User;
import com.qiange.service.AddressBookService;
import com.qiange.service.OrderDetailService;
import com.qiange.service.OrdersService;
import com.qiange.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {

        ordersService.submit(orders);

        return Result.success("提交订单成功");
    }

    @GetMapping("/userPage")
    public Result<Page> userPage(Integer page, Integer pageSize) {

        Long userId = BaseContext.getId();

        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, userId);
        lqw.orderByDesc(Orders::getOrderTime);

        ordersService.page(ordersPage, lqw);

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> orders = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = orders.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);

            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderDetail::getOrderId, ordersDto.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);

            ordersDto.setOrderDetails(orderDetailList);

            return ordersDto;

        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);

        return Result.success(ordersDtoPage);
    }

    @GetMapping("/page")
    public Result<Page> selectByPage(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        ordersLambdaQueryWrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);

        ordersService.page(ordersPage, ordersLambdaQueryWrapper);

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> orders = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = orders.stream().map((orderItem) -> {
            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(orderItem, ordersDto);

            ordersDto.setUserName(orderItem.getConsignee());

            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);


        return Result.success(ordersDtoPage);
    }

    @PutMapping
    public Result<String> updateStatus(@RequestBody Orders orders) {

        LambdaUpdateWrapper<Orders> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(orders.getId() != null, Orders::getId, orders.getId());
        lqw.set(orders.getStatus() != null, Orders::getStatus, orders.getStatus());

        ordersService.update(orders, lqw);

        return Result.success("更改状态成功");
    }
}
